/**
 * @author Liu Cong
 */

FBL.ns(function() { with (FBL) {

// ************************************************************************************************
// Model implementation

Firebug.FireAMF = extend(Firebug.Module,
{
    dispatchName: "fireamf",
    initialize: function()
    {
        Firebug.NetMonitor.NetInfoBody.addListener(this);

        // Used by Firebug.DOMPanel.DirTable domplate.
        this.toggles = {};
    },

    shutdown: function()
    {
        Firebug.NetMonitor.NetInfoBody.removeListener(this);
    },

    initTabBody: function(infoBox, file)
    {
        var contentType = safeGetContentType(file);
        if (!contentType)
            return;

        if (contentType.indexOf("application/x-amf") != 0)
            return;
        try{
			file.AMFObject = file;
        }catch(e){
			return;
		}

        // Check the file.request content-type and display
        // the tab only when appropriate.
        if (file.AMFObject)
            Firebug.NetMonitor.NetInfoBody.appendTab(infoBox, "AMF",
                $STR("fireamf.tab.AMF"));
    },

    // Update listener for TabView
    updateTabBody: function(infoBox, file, context)
    {
        var tab = infoBox.selectedTab;
        var tabBody = getElementByClass(infoBox, "netInfoAMFText");
        if (!hasClass(tab, "netInfoAMFTab") || tabBody.updated)
            return;

        tabBody.updated = true;

        if (file.AMFObject) {
            Firebug.DOMPanel.DirTable.tag.replace(
                 {object: file.AMFObject, toggles: this.toggles}, tabBody);
        }
    },

});

function safeGetContentType(file)
{
    try
    {
        return new String(file.request.contentType).toLowerCase();
    }
    catch (err)
    {
    }

    return null;
}
function hfdumpobj(obj){
    switch(typeof obj){
        case 'object':
            if(obj == null) return obj;
            if(obj instanceof XMLHttpRequest){
                var statusLine = '', headersLine = '', responseLine = '';
                try{
                    statusLine =  obj.status+' '+obj.statusText+'\n';
                    headersLine = obj.getAllResponseHeaders()+'\n';
                    responseLine = obj.responseText;
                }catch(e){}
                return statusLine+headersLine+responseLine;
            } else if(obj instanceof Array){
                return '[' + [hfdumpobj(p) for each(p in obj)].join(', ') + ']';
            } else {
                return '{' + [hfdumpobj(p)+': '+hfdumpobj(obj[p]) for(p in obj)].join(', ') + '}';
            }
            break;
        case 'function':
            return obj.toSource();
            break;
        case 'string':
            return '"' + obj.replace(/"/g, '\\"') + '"';
            break;
        case 'undefined':
        case 'boolean':
        case 'number':
        default:
            return obj;
            
    }
}


// ************************************************************************************************
// Registration

Firebug.registerModule(Firebug.FireAMF);
}});
